#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    socketComm = new SocketComm("192.168.10.200", 2345);
    // socketComm = new SocketComm("127.0.0.1", 5020);

    // ========== 光开关控制 ==========
    connect(socketComm, &SocketComm::SendLightChannelOn, this, &MainWindow::onSendLightChannelOn);
    // ========== 回零/位置控制 (FUS) ==========
    connect(socketComm, &SocketComm::SendFocusMove, this, &MainWindow::onSendFocusMove);
    connect(socketComm, &SocketComm::SendFocusSetok, this, &MainWindow::onSendFocusSetok);
    connect(socketComm, &SocketComm::SendFocusWarn, this, &MainWindow::onSendFocusWarn);
    // ========== 风扇控制 (FAN) ==========
    connect(socketComm, &SocketComm::SendFanTem, this, &MainWindow::onSendFanTem);
    connect(socketComm, &SocketComm::SendFanSetok, this, &MainWindow::onSendFanSetok);
    // ========== 激光器温度控制 (LAS) ==========
    connect(socketComm, &SocketComm::SendLaserTemSetok, this, &MainWindow::onSendLaserTemSetok);
    connect(socketComm, &SocketComm::SendLaserTem, this, &MainWindow::onSendLaserTem);
    // ========== 光纤炉温度控制(大) (FTC) ==========
    connect(socketComm, &SocketComm::SendStoveTemSetok, this, &MainWindow::onSendStoveTemSetok);
    connect(socketComm, &SocketComm::SendStoveTemSetErr, this, &MainWindow::onSendStoveTemSetErr);
    connect(socketComm, &SocketComm::SendStoveTemRead, this, &MainWindow::onSendStoveTemRead);
    connect(socketComm, &SocketComm::SendStoveTemReadErr, this, &MainWindow::onSendStoveTemReadErr);
    // ========== 光纤炉温度 ==========
    connect(socketComm, &SocketComm::SendTemAB, this, &MainWindow::onSendTemAB);
    // ========== 温度、湿度、气压 ==========
    connect(socketComm, &SocketComm::SendEnvThree, this, &MainWindow::onSendEnvThree);

    socketComm->ConnectToMain();
}

MainWindow::~MainWindow()
{
    socketComm->disConnectToMain();
    delete socketComm;
    delete ui;
}


// socketComm信号连接的槽函数
void MainWindow::onSendLightChannelOn(int channelIndex)
{
    ui->lcdNumber_light_reCh->display(channelIndex);
}
void MainWindow::onSendFocusMove(bool ok)
{
    ui->lcdNumber_focus_reMove->display(ok ? 1 : 0);
}
void MainWindow::onSendFocusSetok(bool ok)
{
    ui->lcdNumber_focus_reSet->display(ok ? 1 : 0);
}
void MainWindow::onSendFocusWarn(bool ok)
{
    ui->lcdNumber_focus_reWarn->display(ok ? 1 : 0);
}
void MainWindow::onSendFanSetok(bool ok)
{
    ui->lcdNumber_fan_reSet->display(ok ? 1 : 0);
}
void MainWindow::onSendFanTem(int temp)
{
    ui->lcdNumber_fan_readTem->display(temp);
}
void MainWindow::onSendLaserTemSetok(bool ok)
{
    ui->lcdNumber_raser_reSet->display(ok ? 1 : 0);
}
void MainWindow::onSendLaserTem(double temp)
{
    ui->lcdNumber_raser_readTem->display(temp);
}
void MainWindow::onSendStoveTemSetok(bool ok)
{
    ui->lcdNumber_stoveBig_reSet->display(ok ? 1 : 0);
}
void MainWindow::onSendStoveTemSetErr(bool ok)
{
    ui->lcdNumber_stoveBig_reSetErr->display(ok ? 1 : 0);
}
void MainWindow::onSendStoveTemRead(int channel, double temp)
{
    ui->lcdNumber_stoveBig_reReadCha->display(channel);
    ui->lcdNumber_stoveBig_readTem->display(temp);
}
void MainWindow::onSendStoveTemReadErr(bool ok)
{
    ui->lcdNumber_stoveBig_reReadErr->display(ok ? 1 : 0);
}
void MainWindow::onSendTemAB(double tempA, double tempB)
{
    ui->lcdNumber_stove_temA->display(tempA);
    ui->lcdNumber_stove_temB->display(tempB);
}
void MainWindow::onSendEnvThree(double temp, double humidity, double pressure)
{
    ui->lcdNumber_env_tem->display(temp);
    ui->lcdNumber_env_humidity->display(humidity);
    ui->lcdNumber_env_press->display(pressure);
}

// ========== 灯光控制 ==========
void MainWindow::on_pushButton_lamp_laserOpen_clicked()
{
    socketComm->setLaserOn(1);
}
void MainWindow::on_pushButton_lamp_laserClose_clicked()
{
    socketComm->setLaserOn(0);
}
void MainWindow::on_pushButton_lamp_redOpen_clicked()
{
    socketComm->setRedLight(1);
}
void MainWindow::on_pushButton_lamp_redClose_clicked()
{
    socketComm->setRedLight(0);
}
void MainWindow::on_pushButton_lamp_greenOpen_clicked()
{
    socketComm->setGreenLight(1);
}
void MainWindow::on_pushButton_lamp_greenClose_clicked()
{
    socketComm->setGreenLight(0);
}
void MainWindow::on_pushButton_lamp_blueOpen_clicked()
{
    socketComm->setBlueLight(1);
}
void MainWindow::on_pushButton_lamp_blueClose_clicked()
{
    socketComm->setBlueLight(0);
}
// ========== 衰减器控制 ==========
void MainWindow::on_pushButton_atten_open_clicked()
{
    socketComm->setVOAOn(1);
}
void MainWindow::on_pushButton_atten_close_clicked()
{
    socketComm->setVOAOn(0);
}
void MainWindow::on_pushButton_atten_setVol_clicked()
{
    int voltage = ui->lineEdit_atten_setVol->text().toInt();
    socketComm->setVOAVoltage(voltage);
}
// ========== 光开关控制 ==========
void MainWindow::on_pushButton_light_open_clicked()
{
    socketComm->selectOpticalChannel(ui->comboBox_light_ch->currentIndex());
}
// ========== 回零/位置控制 (FUS) ==========
void MainWindow::on_pushButton_focus_home_clicked()
{
    socketComm->startHome();
}
void MainWindow::on_pushButton_focus_stop_clicked()
{
    socketComm->stopMotion();
}
void MainWindow::on_pushButton_focus_posStart_clicked()
{
    socketComm->startMotion();
}
void MainWindow::on_pushButton_focus_set_clicked()
{
    double speed = ui->lineEdit_focus_speed->text().toDouble();
    double pos = ui->lineEdit_focus_pos->text().toDouble();
    socketComm->startPosition(speed, pos);
}
// ========== 风扇控制 (FAN) ==========
void MainWindow::on_pushButton_fan_setTem_clicked()
{
    int startTem = ui->lineEdit_fan_startTem->text().toInt();
    int fullTem = ui->lineEdit_fan_fullTem->text().toInt();
    socketComm->setFanTemp(startTem, fullTem);
}
void MainWindow::on_pushButton_fan_readTem_clicked()
{
    socketComm->readFanTemp();
}
// ========== 激光器温度控制 (LAS) ==========
void MainWindow::on_pushButton_raser_setTem_clicked()
{
    socketComm->setLaserTemp(ui->lineEdit_raser_tem->text().toDouble());
}
void MainWindow::on_pushButton_raser_searchTem_clicked()
{
    socketComm->queryLaserTemp();
}
// ========== 光纤炉温度控制(大) (FTC) ==========
void MainWindow::on_pushButton_stoveBig_setTem_clicked()
{
    int channel = ui->comboBox_stoveBig_setChannel->currentIndex() + 1;
    double temp = ui->lineEdit_stoveBig_tem->text().toDouble();
    socketComm->setFurnaceTemp(channel, temp);
}
void MainWindow::on_pushButton_stoveBig_searchTem_clicked()
{
    socketComm->queryFurnaceTemp(ui->comboBox_stoveBig_readChannel->currentIndex() + 1);
}
// ========== 光纤炉温度 ==========
void MainWindow::on_pushButton_stove_search_clicked()
{
    socketComm->queryFurnaceDualTemp();
}
// ========== 温度、湿度、气压 ==========
void MainWindow::on_lcdNumber_stove_searchEnv_clicked()
{
    socketComm->queryEnvironment();
}
























